package com.gongpingjialoan.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.widget.adapters.ArrayWheelAdapter;
import com.gongpingjialoan.widget.views.WheelView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by Administrator on 2016/12/7.
 */
public class SelectDateDialog extends Dialog implements View.OnClickListener {

    private Context mContext;

    private int day_num;

    private WheelView yearWheelView;

    private ArrayWheelAdapter<String> stringArrayWheelAdapter;

    private String[] datas;

    private OnDialogCallBack mOnDialogCallBack;

    private TextView cancleTextView, confirmTextView;

    private String[] d;

    public SelectDateDialog(Context mContext, int num ,OnDialogCallBack onDialogCallBack) {
        super(mContext, R.style.dialogs);
        this.mContext = mContext;
        this.day_num = num;
        this.mOnDialogCallBack = onDialogCallBack;
    }




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.date_picker_layout);
        initView();
    }



    private String[] getDay() {
        DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
        DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        String[] days = new String[day_num];
        d = new String[day_num];
        for (int i = 0; i < day_num; i++) {
            calendar.add(Calendar.DATE, 1);
            days[i] = df.format(calendar.getTime());
            d[i] = df1.format(calendar.getTime());
        }
        return days;

    }


    private void initView() {
        yearWheelView = (WheelView) findViewById(R.id.year_view);
        cancleTextView = (TextView) findViewById(R.id.btn_myinfo_cancel);
        confirmTextView = (TextView) findViewById(R.id.btn_myinfo_sure);
        cancleTextView.setOnClickListener(this);
        confirmTextView.setOnClickListener(this);

        datas = getDay();
        stringArrayWheelAdapter = new ArrayWheelAdapter<String>(mContext, datas);
        stringArrayWheelAdapter.setItemResource(R.layout.time_tiem);
        stringArrayWheelAdapter.setItemTextResource(R.id.text);
        yearWheelView.setViewAdapter(stringArrayWheelAdapter);

        yearWheelView.setCurrentItem(0);
        yearWheelView.setVisibleItems(3);

        yearWheelView.setCyclic(true);


    }








    @Override
    public void onClick(View view) {
        if (view == cancleTextView) {
            dismiss();
        }
        if (view == confirmTextView) {
            if(mOnDialogCallBack!=null){
                mOnDialogCallBack.onDateBack(d[yearWheelView.getCurrentItem()] , datas[yearWheelView.getCurrentItem()]);
                dismiss();
            }
        }
    }

    public interface  OnDialogCallBack{
        public void onDateBack(String date , String dataDetail);
    }



}
